$(function() {
	
	loadAllData();
	
//	findRoleForSelect("add_type");
//	findRoleForSelect("update_type");

	$('#add').on('hide.bs.modal', function () {
		$("#add_name").val("");
        $("#add_price").val("");
        $("#add_img").val("");
   
	});
	
	$('#update').on('hide.bs.modal', function () {
		$("#update_id").val("");
		$("#update_name").val("");
        $("#update_price").val("");
        $("#update_img").val("");
	});
		
	$("#add_menu").on("click",function () {

		var name = $("#add_name").val();
        var price = $("#add_price").val();
        var type = $("#add_type").val();
		var menu={
			"name":name,"price":price,"type":type
		};
		addData(menu);
	});
	
	$("#update_menu").on("click",function () {
		var id = $("#update_id").val();
		console.log(id);
		var name = $("#update_name").val();
        var price = $("#update_price").val();
        var type = $("#update_type").val();
		var menu={
			"id":id,"name":name,"price":price,"type":type
		};
		modifyData(menu);
	});

	function loadAllData() {
		$.ajax({
			type : "GET",
			url : "/meat/sec/food/list",
			dataType : "json",
			success : function(obj) {
				if(obj.code == 200){
				console.log(obj.data);
				 $("#menus").empty();
				 obj.data.forEach((item)=>{
                     $("#menus").append(
                         "<tr>"+
                         "<th>"+item.id+"</th>"+ 
                         "<th>"+item.name+"</th>"+ 
                         "<th>"+item.price+"</th>"+
//                         "<th><img src="+item.img+"onclick=\"showimage("+item.img+")\"/></td></th>"+
                        "<th><img style='width:40px;height:40px' src="+item.img+" name="+item.id+" class='img'/></td></th>"+
                         "<th>"+item.typeName+"</th>"+
                         "<th><input type='button'   value='Modify' name="+item.id+" class='modify'></th>"+
                         "<th><input type='button'  value='Delete' name="+item.id+" class='delete'></th></tr>"
                     );
                     $("#menus .img[name="+item.id+"]").on("click",function(){
                    	 var src = $(this).attr("src");
         				 $("#modal-img-target").attr("src", src);
         				 $("#modal-img").modal("show");
                     })
        
                     $("#menus .delete[name="+item.id+"]").on("click",function(){
                    	 swal({
               			  title: "Are you sure?",
               			  text: "You will not be able to recover this imaginary data!",
               			  type: "warning",
               			  showCancelButton: true,
               			  confirmButtonColor: "#DD6B55",
               			  confirmButtonText: "Yes, delete it!",
               			  closeOnConfirm: true
               			},
               			function(){
                         deleteData(item.id,item.mid);
               			});
                     });
                     $("#menus .modify[name="+item.id+"]").on("click",function(){
                    	 swal({
               			  title: "Are you sure?",
               			  text: "You will not be able to recover this imaginary data!",
               			  type: "warning",
               			  showCancelButton: true,
               			  confirmButtonColor: "#DD6B55",
               			  confirmButtonText: "Yes, modify it!",
               			  closeOnConfirm: true
               			},
               			function(){
               			  loadDataById(item.id,item.mid);
                       	  $('#update').modal('show'); 
               			});

                     });
      

                 })
				}else{
					swal("DB is wrong!");
				}
			}
		});
	}
	
	function loadDataById(id,mid) {
// confirm_ = confirm('This action will delete current order! Are you sure?');
		$.ajax({
			 type: "POST",
             url: "/meat/sec/food/goup?id="+id+"&mid="+mid,
             dataType:"json",
             contentType:"application/json",
 
			success : function(obj) {
				if(obj.code == 200){
				console.log(obj.data);
                     $("#update_id").val(obj.data.id);
                     $("#update_name").val(obj.data.name);
                     $("#update_price").val(obj.data.price); 
                     $("#update_imgpath").val(obj.data.img); 
				} else if(obj.code == 500){
					swal(obj.msg);
				}
			}
		});
	}
	
	
	function deleteData(id,mid){	
			  $.ajax({
	                 type: "POST",
	                 url: "/meat/sec/food/del?id="+id+"&mid="+mid,
	                 dataType:"json",
	                 contentType:"application/json",
	                 success : function(obj) {
	                	 if(obj.code == 200){
	                		 swal("Deleted!", "Your imaginary file has been deleted.", "success");
	                		 loadAllData();     
	         				}else if(obj.code == 500){
	        					swal(obj.msg);
	        				}
	                 }
	            });

     }

	
	function findRoleForSelect(type) {
        $.ajax({
            url:"/meat/sec/food/type",
            type: "GET",
            dataType: "json",
            success: function (obj) {
            	var data = obj.data;
                var len = data.length;
                $("#"+type).find('option').remove();
                for (var i = 0; i < len; i++) {
                    $("#"+type).append('<option value=' + data[i].id + '>' + data[i].name + '</option>');
                }
                var optionString = "";
                for (i = 0; i < len; i++) {
                    optionString += "<option value=\'"+ data[i].id +"\'>" + data[i].name + "</option>";
                }
                var myobj = document.getElementById(type);
                if (myobj.options.length == 0)
                {
                    $("#"+type).html(optionString);
                    $("#"+type).selectpicker('refresh');
                }
            }
        })
    }

//	function showimage(source)
//    {
//		alert(source);
//        $("#ShowImage_Form").find("#img_show").html("<image src='"+source+"' class='carousel-inner img-responsive img-rounded' />");
//        $("#ShowImage_Form").modal();
//    }
	
	
		
	
	function addData(menu) {
		
		$.ajaxFileUpload({  
        	url : "/meat/sec/food/add",// servlet请求路径
        	secureuri : false,  
        	fileElementId : 'add_img',// 上传控件的id
        	dataType : 'json',  
        	data : menu, // 其它请求参数
        	success : function(obj, status) {  
        		console.log("ddd");
        		if(obj.code ==200){
        			swal("Add success!");  		
        		} else{
        			swal("Add Fail!");  
        		} 
        		$('#add').modal('hide');
    			loadAllData();
        	},  
        	error : function(data, status, e) { 
        		swal("Upload error!");  
        	}  
    	});

	};
//	
//	function modifyData(menu) {
//		$.ajaxFileUpload({  
//        	url : "/meat/sec/food/fmodify",// servlet请求路径
//        	secureuri : false,  
//        	fileElementId : 'update_img',// 上传控件的id
//        	dataType : 'json',  
//        	data : menu, // 其它请求参数
//        	success : function(obj, status) {  
//        		console.log("ddd");
//        		if(obj.code ==200){
//        			swal("Modify success!");	
//        		} else{
//        			swal("Modify Fail!");
//        		} 
//        		$('#update').modal('hide');
//    			loadData();
//        	},  
//        	error : function(data, status, e) {  
//        		swal("Upload error!");  
//        	}  
//    	});
//
//	};
	
	function modifyData(menu) {
		$.ajaxFileUpload({  
		         url : "/meat/sec/food/fmodify",// servlet请求路径
		         secureuri : false,  
		         data : menu, // 其它请求参数
		         fileElementId : 'update_img',// 上传控件的id
		         dataType : 'json',  
		         success : function(obj, status) {  
		         console.log("ddd");
		         if(obj.code ==200){
		         swal("Modify success!"); 
		         } else{
		         swal("Modify Fail!");
		         } 
		         $('#update').modal('hide');
		         loadAllData();
		         },  
		         error : function(data, status, e) {  
		         swal("Upload error!");  
		         }  
		     });

		};
	 	  
});
